/*
* Copyright (c) 2008, German Buela
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*     * Redistributions of source code must retain the above copyright
*       notice, this list of conditions and the following disclaimer.
*     * Redistributions in binary form must reproduce the above copyright
*       notice, this list of conditions and the following disclaimer in the
*       documentation and/or other materials provided with the distribution.
*     * The name of German Buela may not be used to endorse or promote products
*       derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY GERMAN BUELA "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL GERMAN BUELA BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package org.gbuela.avant;

import java.util.Map;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

/**
 * Http specific implementation for Request. While there is only
 * a HTTP servlet implementation for Avant's front controller, this is the only
 * request implementation used. It is a wrapper for HttpServletRequest.
 * 
 * @author gbuela
 */
public class HttpRequest implements Request {
    private HttpServletRequest request = null;
    
    /**
     * Creates a new instance of HttpRequest
     */
    public HttpRequest() {
    }

    public HttpRequest(HttpServletRequest request) {
        this.setRequest(request);
    }
    
    public void setRequest(HttpServletRequest request) {
        this.request = request;
    }
    public HttpServletRequest getRequest() {
        return this.request;
    }    

    public String getParameter(String parName)
    {
        if(request == null) throw new IllegalStateException(Constants.REQUEST_NULL);
        if(parName == null) throw new IllegalArgumentException(Constants.PARAM_NAME_NULL);

        return request.getParameter(parName);
    }
    
    /**
     * Returns an attribute from either the request, session or servlet context (whichever
     * is found first, in that order, or null if not found).
     * @param attName attribute name
     * @return the attribute of that name
     */
    public Object getAttribute(String attName)
    {
        if(request == null) throw new IllegalStateException(Constants.REQUEST_NULL);
        if(attName == null) throw new IllegalArgumentException(Constants.ATTR_NAME_NULL);
        
        Object o = request.getAttribute(attName);
        if(o == null)
        {
            o = request.getSession().getAttribute(attName);
            if(o == null)
            {
                o = request.getSession().getServletContext().getAttribute(attName);
            }
        }
        return o;
    }
    
    public void setAttribute(String attName, Object attValue)
    {
        if(request == null) throw new IllegalStateException(Constants.REQUEST_NULL);
        if(attName == null) throw new IllegalArgumentException(Constants.ATTR_NAME_NULL);
        
        if(attValue == null)
            request.removeAttribute(attName);
        else
            request.setAttribute(attName, attValue);
    }
    public void setSessionAttribute(String attName, Object attValue)
    {
        if(request == null) throw new IllegalStateException(Constants.REQUEST_NULL);
        if(attName == null) throw new IllegalArgumentException(Constants.ATTR_NAME_NULL);

        if(attValue == null)
            request.getSession().removeAttribute(attName);
        else
            request.getSession().setAttribute(attName, attValue);
    }
    public void setGlobalAttribute(String attName, Object attValue)
    {
        if(request == null) throw new IllegalStateException(Constants.REQUEST_NULL);
        if(attName == null) throw new IllegalArgumentException(Constants.ATTR_NAME_NULL);

        if(attValue == null)
            request.getSession().getServletContext().removeAttribute(attName);
        else
            request.getSession().getServletContext().setAttribute(attName, attValue);
    }

    public Object getSessionAttribute(String attName) {
        if(request == null) throw new IllegalStateException(Constants.REQUEST_NULL);
        if(attName == null) throw new IllegalArgumentException(Constants.ATTR_NAME_NULL);

        return request.getSession().getAttribute(attName);
    }

    public Object getGlobalAttribute(String attName) {
        if(request == null) throw new IllegalStateException(Constants.REQUEST_NULL);
        if(attName == null) throw new IllegalArgumentException(Constants.ATTR_NAME_NULL);

        return request.getSession().getServletContext().getAttribute(attName);
    }

    public Map getParameterMap() {
        if(request == null) throw new IllegalStateException(Constants.REQUEST_NULL);
        return request.getParameterMap();
    }

    public String getContextPath() {
        return request.getContextPath();
    }
    
    public String getRequestURL() {
        return request.getRequestURL().toString();
    }

    public ServletRequest getServletRequest()
    {
        return request;
    }
    
}
